/**************************************************************************
*
* Tint Task Manager
* 
* Copyright (C) 2007 Pål Staurland (staura@gmail.com)
* 
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Soft-
* ware"), to deal in the Software without restriction, including without
* limitation the rights to use, copy, modify, merge, publish, distribute,
* sublicense, and/or sell copies of the Software, and to permit persons to
* whom the Software is furnished to do so, subject to the following condi-
* tions:
* 
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
* 
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABIL-
* ITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT
* SHALL THE AUTHOR BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT
* OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
* 
**************************************************************************/

#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/Xatom.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <glib.h>
#include "config.h"
#include "server.h"

int hex_char_to_int (char c)
{
    int r;
    
    if (c >= '0' && c <= '9')
		r = c - '0';
    else if (c >= 'a' && c <= 'f')
		r = c - 'a' + 10;
    else if (c >= 'A' && c <= 'F')
		r = c - 'A' + 10;
    else
		r = 0;

    return r;
}

int hex_to_rgb (char *hex, int *r, int *g, int *b)
{
    int len;

    if (hex == NULL || hex[0] != '#')
		return (0);

    len = strlen (hex);
    if (len == 3 + 1) 
    {
		*r = hex_char_to_int (hex[1]);
		*g = hex_char_to_int (hex[2]);
		*b = hex_char_to_int (hex[3]);
    } 
    else if (len == 6 + 1) 
    {
		*r = hex_char_to_int (hex[1]) * 16 + hex_char_to_int (hex[2]);
		*g = hex_char_to_int (hex[3]) * 16 + hex_char_to_int (hex[4]);
		*b = hex_char_to_int (hex[5]) * 16 + hex_char_to_int (hex[6]);
    } 
    else if (len == 12 + 1) 
    {
		*r = hex_char_to_int (hex[1]) * 16 + hex_char_to_int (hex[2]);
		*g = hex_char_to_int (hex[5]) * 16 + hex_char_to_int (hex[6]);
		*b = hex_char_to_int (hex[9]) * 16 + hex_char_to_int (hex[10]);
    } 
    else
		return 0;

    return 1;
}

void get_color (char *hex, double *rgb)
{
	int r, g, b;
	hex_to_rgb (hex, &r, &g, &b);
	
	rgb[0] = (r / 255.0);
	rgb[1] = (g / 255.0);
	rgb[2] = (b / 255.0);
}

void add_entry (char *key, char *value)
{
	/* Font */
	if (strcmp (key, "font") == 0)
		config.font = strdup (value);
	else if (strcmp (key, "font_color") == 0)
		get_color (value, config.font_color);
	else if (strcmp (key, "font_alpha") == 0)
		config.font_alpha = (atoi (value) / 100.0);	
	else if (strcmp (key, "font_active_color") == 0)
		get_color (value, config.font_active_color);
	else if (strcmp (key, "font_active_alpha") == 0)
		config.font_active_alpha = (atoi (value) / 100.0);	
	else if (strcmp (key, "font_shadow") == 0)
		config.font_shadow = atoi (value);	
		
	/* Task */
	else if (strcmp (key, "task_text_centered") == 0)
		config.task_text_centered = atoi (value);	
	else if (strcmp (key, "task_width") == 0)
		config.task_width = atoi (value);
	else if (strcmp (key, "task_margin") == 0)
		config.task_margin = atoi (value);	
	else if (strcmp (key, "task_padding") == 0)
		config.task_padding = atoi (value);	
	
	/* Panel */
	else if (strcmp (key, "panel_width") == 0)
		config.panel_width = atoi (value);	
	else if (strcmp (key, "panel_height") == 0)
		config.panel_height = atoi (value);	
	else if (strcmp (key, "panel_tasks_centered") == 0)
		config.panel_tasks_centered = atoi (value);
	
	/* Icon */
	else if (strcmp (key, "icon") == 0)
		config.icon = atoi (value);	
	else if (strcmp (key, "icon_size") == 0)
		config.icon_size = atoi (value);		
		
	/* Panel Background */
	else if (strcmp (key, "panel_background") == 0)
		config.panel_background = atoi (value);
	else if (strcmp (key, "panel_background_color") == 0)
		get_color (value, config.panel_background_color);
	else if (strcmp (key, "panel_background_alpha") == 0)
		config.panel_background_alpha = (atoi (value) / 100.0);
			
	/* Task Background */
	else if (strcmp (key, "task_background") == 0)
		config.task_background = atoi (value);
	else if (strcmp (key, "task_background_color") == 0)
		get_color (value, config.task_background_color);
	else if (strcmp (key, "task_background_alpha") == 0)
		config.task_background_alpha = (atoi (value) / 100.0);		
	else if (strcmp (key, "task_active_background_color") == 0)	
		get_color (value, config.task_active_background_color);
	else if (strcmp (key, "task_active_background_alpha") == 0)
		config.task_active_background_alpha = (atoi (value) / 100.0);	
	else if (strcmp (key, "task_background_as_border") == 0)
		config.task_background_as_border = atoi (value);
		
	else
		fprintf(stderr, "Invalid option: \"%s = %s\", using default value - please correct your config file\n", key, value);
}

void set_defaults ()
{
	/* Font */
	config.font = "sans bold 8";
	get_color ("#ffffff", config.font_color);
	config.font_alpha = 0.5;
	get_color ("#ffffff", config.font_active_color);
	config.font_active_alpha = 0.75;
	config.font_shadow = 1;

	/* Task */
	config.task_text_centered = 0;
	config.task_width = 100;
	config.task_margin = 3;
	config.task_padding = 5;

	/* Panel */
	config.panel_width = 0;
	config.panel_height = 30;
	config.panel_tasks_centered = 0;
	
	/* Icon */
	config.icon = 1;
	config.icon_size = 16;

	/* Panel Background */
	config.panel_background = 1;
	get_color ("#000000", config.panel_background_color);
	config.panel_background_alpha = 0.25;

	/* Task Background */
	config.task_background = 1;
	get_color ("#ffffff", config.task_background_color);
	config.task_background_alpha = 0.25;
	get_color ("#ffffff", config.task_active_background_color);
	config.task_active_background_alpha = 0.50;
	config.task_background_as_border = 0;
}

int parse_line (const char *line, char **name, char **value)
{
	char *a, *b;

	if (!(a = strchr (line, '=')))
		return 0;
		
	/* overwrite '=' with '\0' */
	a[0] = '\0';
	*name = strdup (line);
	a++;

	/* overwrite '\n' with '\0' if '\n' present */
	if ((b = strchr (a, '\n')))
		b[0] = '\0';

	*value = strdup (a);

	return 1;
}

void config_finish ()
{
	if (!config.panel_width)
		config.panel_width = server.screen_width;
	config.task_show_text = 1;
}

void config_read ()
{
	char line[80], *config_path;
	FILE *fp;

 	config_path = g_build_filename (g_get_home_dir(), ".config", "tint", "tintrc", NULL);
	fp = fopen(config_path, "r");
		
	set_defaults ();
	
	if (!fp)
		fprintf(stderr, "Cannot open config file! Using defaults...\nCopy config_sample to ~/.config/tint/tintrc\n");
	else
	{
		while(fgets(line, 80, fp) != NULL)
		{
			char *key, *value;

			/* Skip comments */
			if ((line[0] == '#'))
				continue; 

			/* Skip blank lines */
			if ((line[0] == '\n'))
				continue; 
			
			/* Skip useless lines */
			if (strchr(line, '=') == NULL)
				continue;

			if (!parse_line (line, &key, &value))
			{
				continue;
			}

			g_strstrip(key);
			g_strstrip(value);
			
			/* Add entry */
			add_entry (key, value);
			
			free (key);
			free (value);
		}
		
		fclose (fp);
	}
}
